'use strict';

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var vue = new Vue({
    data: {
        form: {
            itemid: 0,
            catid: 0,
            title: '',
            content: '',
            thumb: '',
            flag: '1'
        },
        category_select: []
    },
    methods: {
        set_header: function set_header() {
            return {
                Authorization: JSON.stringify({
                    auth: Cookies.get('auth'),
                    userid: Cookies.get('userid'),
                    username: Cookies.get('username'),
                    groupid: Cookies.get('groupid')
                })
            };
        },
        handleSuccess: function handleSuccess(res, file) {
            this.form.thumb = res.data.url;
        },
        handleError: function handleError(error, file) {
            console.log(error, file);
            this.$message.error(error.toString());
        },
        handleRemove: function handleRemove() {
            this.form.thumb = '';
        },
        onSubmit: function () {
            var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
                var response, data;
                return regeneratorRuntime.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                //同步content
                                this.form.content = tinymce.editors['content'].getContent();

                                if (this.form.catid) {
                                    _context.next = 4;
                                    break;
                                }

                                this.$message({
                                    type: 'error',
                                    message: '所属分类不能为空'
                                });
                                return _context.abrupt('return');

                            case 4:
                                if (this.form.title) {
                                    _context.next = 7;
                                    break;
                                }

                                this.$message({
                                    type: 'error',
                                    message: '标题不能为空'
                                });
                                return _context.abrupt('return');

                            case 7:
                                if (this.form.content) {
                                    _context.next = 10;
                                    break;
                                }

                                this.$message({
                                    type: 'error',
                                    message: '内容不能为空'
                                });
                                return _context.abrupt('return');

                            case 10:
                                if (this.form.flag) {
                                    _context.next = 13;
                                    break;
                                }

                                this.$message({
                                    type: 'error',
                                    message: '状态不能为空'
                                });
                                return _context.abrupt('return');

                            case 13:
                                _context.prev = 13;
                                _context.next = 16;
                                return axios.post('/api/article/mod', this.form);

                            case 16:
                                response = _context.sent;
                                data = response.data;

                                if (parseInt(data.code) > 0) {
                                    this.$message({
                                        message: data.msg,
                                        type: "success",
                                        duration: 1000
                                    });
                                    setTimeout(function () {
                                        location.reload();
                                    }, 1000);
                                } else {
                                    this.$message.error(data.msg);
                                }
                                _context.next = 24;
                                break;

                            case 21:
                                _context.prev = 21;
                                _context.t0 = _context['catch'](13);

                                this.$message.error(_context.t0.toString());

                            case 24:
                            case 'end':
                                return _context.stop();
                        }
                    }
                }, _callee, this, [[13, 21]]);
            }));

            function onSubmit() {
                return _ref.apply(this, arguments);
            }

            return onSubmit;
        }()
    },
    created: function created() {},
    mounted: function () {
        var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
            var params, response, data, _response, _data;

            return regeneratorRuntime.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:

                            this.$check_login('article', 'mod');

                            tinymce.init(Object.assign(tinymceConfig, { selector: '#content' }));

                            params = Qs.parse(window.location.search.substr(1));

                            if (!(typeof params.itemid != 'undefined' && parseInt(params.itemid) > 0)) {
                                _context2.next = 23;
                                break;
                            }

                            _context2.prev = 4;
                            _context2.next = 7;
                            return axios.post('/api/article/preview', { itemid: params.itemid });

                        case 7:
                            response = _context2.sent;
                            data = response.data;

                            this.form.itemid = data.data.itemid;
                            this.form.catid = data.data.catid;
                            this.form.title = data.data.title;
                            this.form.content = data.data.content;
                            this.form.thumb = data.data.thumb;
                            this.form.flag = String(data.data.flag);
                            tinymce.editors['content'].setContent(data.data.content);

                            _context2.next = 21;
                            break;

                        case 18:
                            _context2.prev = 18;
                            _context2.t0 = _context2['catch'](4);

                            this.$message.error(_context2.t0.toString());

                        case 21:
                            _context2.next = 24;
                            break;

                        case 23:
                            this.$message.error('lose itemid');

                        case 24:
                            _context2.prev = 24;
                            _context2.next = 27;
                            return axios.post('/api/article/category_lst', {});

                        case 27:
                            _response = _context2.sent;
                            _data = _response.data;

                            if (parseInt(_data.code) > 0) {
                                this.category_select = _data.data;
                            } else {
                                this.$message.error(_data.msg);
                            }
                            _context2.next = 35;
                            break;

                        case 32:
                            _context2.prev = 32;
                            _context2.t1 = _context2['catch'](24);

                            this.$message.error(_context2.t1.toString());

                        case 35:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, this, [[4, 18], [24, 32]]);
        }));

        function mounted() {
            return _ref2.apply(this, arguments);
        }

        return mounted;
    }()
}).$mount('#app');